package com.dyc.caradvertisting.ui.fmt.tab.store.gd;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dyc.caradvertisting.R;
import com.dyc.caradvertisting.bean.req.GoodsDetails;
import com.dyc.caradvertisting.bean.resp.GoodsD;
import com.dyc.caradvertisting.comm.GsonUtils;
import com.njh.tools.api.manager.RetrofitManager;
import com.njh.tools.base.BaseFragment;
import com.njh.tools.common.img.GlideUtils;
import com.njh.tools.common.userInfo.SPUserInfo;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnLoadImageListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 商品详情
 */
public class GoodsDFmt extends BaseFragment<GoodsDPresenter, GoodsDModel> implements GoodsDContract.View {
    private static final String ARG_PARAM1 = "param1";
    @BindView(R.id.ban_adv)
    Banner banAdv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_kc_number)
    TextView tvKcNumber;
    @BindView(R.id.tv_goods_address)
    TextView tvGoodsAddress;
    @BindView(R.id.tv_sp_number)
    TextView tvSpNumber;

    // TODO: Rename and change types of parameters
    private String mParam1;


    public GoodsDFmt() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @return A new instance of fragment GoodsDFmt.
     */
    // TODO: Rename and change types and number of parameters
    public static GoodsDFmt newInstance(String param1) {
        GoodsDFmt fragment = new GoodsDFmt();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return getView(inflater, container);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_goods_dfmt;
    }

    @Override
    public void initView(View baseView) {
        ButterKnife.bind(this, baseView);
        GoodsDetails goodsDetails = new GoodsDetails();
        goodsDetails.setUid(SPUserInfo.getUid(this.getContext()));
        goodsDetails.setGoodsId(mParam1);
        mPresenter.reqGoodsDetails(GsonUtils.getmGson().toJson(goodsDetails));
    }

    @Override
    public boolean isSwipeBack() {
        return false;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel, this._mActivity);
    }

    @Override
    public void initData(GoodsD goodsD) {
        if (null != goodsD) {
            initToolbar(goodsD.getName());
            if (null != goodsD.getBanner() && null != goodsD.getBanner().getFile()) {
                List<String> paths = goodsD.getBanner().getFile();
                for (int i = 0; i < paths.size(); i++) {
                    paths.set(i, RetrofitManager.baseUrl + paths.get(i));
                }
                banAdv.setDelayTime(4500);
                banAdv.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
                banAdv.setIndicatorGravity(BannerConfig.CENTER);

                banAdv.setImages(paths, new OnLoadImageListener() {
                    @Override
                    public void OnLoadImage(ImageView view, Object url) {
                        GlideUtils.displayImage(GoodsDFmt.this.getContext(),url.toString(),view);
                    }
                });
            }
            setTextValue(tvTitle,goodsD.getName());
            setTextValue(tvPrice,"¥" +goodsD.getPrice());
            setTextValue(tvKcNumber,"库存"+goodsD.getStock()+"件");
            setTextValue(tvGoodsAddress,"货源:"+goodsD.getSource());
            setTextValue(tvSpNumber,"已有"+goodsD.getBuy()+"人购买");
        }
    }

    @Override
    public void finish() {
        _mActivity.onBackPressed();
    }
}
